<?php
/**
 * @category   Application_Extensions
 * @package    Groupon Plugin
 * @copyright  VinaTay Co., Ltd.
 * @license
 */
class Exam_Model_DbTable_Categories extends Engine_Db_Table {
    protected $_rowClass = 'Exam_Model_Category';
    
    public function getParentCategory() {
        $table=Engine_Api::_()->getDbTable('categories','Exam');
        return $table->fetchAll($table->select()->where('parent_id = 0')->order('category_id ASC'));
    }
    public function getSubCategory($parent_id){
        $table=Engine_Api::_()->getDbTable('categories','Exam');
        return $table->fetchAll($table->select()->where('parent_id=?',$parent_id)->order('category_name ASC'));
    }
    public function  getCategoryList($id = 0, $arrCategolyIdAfter = null, $space = '') 
	{
		if(is_null($arrCategolyIdAfter) || !is_array($arrCategolyIdAfter)){
			$arrCategolyIdAfter = array();
		}
		
		// Array all news category that is left
        $arrCategolyIdFirst = array();

        // Get all items of parent
//        $result = $this->getItems(array('parent_id' => $id));
        if(is_null($id)){
            $id = -1;
        }
        $result = $this->select()
                    ->where('parent_id = ?', $id)
                    ->query()->fetchAll();

       	if(count($result) > 0) {
        	for($j =0 ; $j < count($result) ; $j++) {
        		$result[$j]['category_name'] = $space. $result[$j]['category_name'];
            	$arrCategolyIdAfter[$result[$j]['category_id']]= $result[$j];
               	$arrCategolyIdAfter = $this->getCategoryList( $result[$j]['category_id'], $arrCategolyIdAfter, $space);
            }
         }
//      if(count($result) > 0) {
//        	for($j =0 ; $j < count($result) ; $j++) {
//        		$result[$j]->category_name = $space. $result[$j]->category_name;
//            	$arrCategolyIdAfter[$result[$j]->category_id]= $result[$j];
//               	$arrCategolyIdAfter = $this->getCategoryList( $result[$j]->category_id, $arrCategolyIdAfter, $space);
//            }
//         }
      return $arrCategolyIdAfter;
    }
    

}